package JDBC_Test.book.Service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
//import JDBC_Test.StreamUtils.StreamUtils;
//import JDBC_Test.Student.Model.StudentModel;abstract
import java.util.UUID;

import JDBC_Test.book.Dao.BookDao;
import JDBC_Test.book.Dao.BookStockDao;
import JDBC_Test.book.Dao.CardDao;
import JDBC_Test.book.Dao.RecordDao;
import JDBC_Test.book.Model.BookModel;
import JDBC_Test.book.Model.BookStockModel;
import JDBC_Test.book.Model.BorrowRecordModel;
import JDBC_Test.book.Model.CardModel;

/**
 * @author 姜柏宇 题目8
 */
public class BookService {

	BookDao bookDao = new BookDao();
	BookStockDao bookStockDao = new BookStockDao();
	CardDao cardDao = new CardDao();
	RecordDao recordDao = new RecordDao();

	public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {

		BookService bookService = new BookService() {
		};

		bookService.print();
		Scanner scanner = new Scanner(System.in);
		while (true) {

			System.out.println("请键入操作码：");
			String s4 = scanner.nextLine();
			int n = Integer.valueOf(s4);

			switch (n) {
			case 0:
				System.out.println("感谢使用 再见");
				break;
			case 1:
				bookService.insertBook();
				break;
			case 2:
				bookService.insertCard();
				break;
			case 3:
				bookService.borrow();
				break;
			case 4:
				bookService.returnbook();
				break;
			case 5:
				bookService.selectByBookId();
				break;
			case 6:
				bookService.selectByCardId();
				break;
			case 7:
				bookService.selectByCount();
				break;
			default:
				break;
			}

			// scanner.close();
		}
	}

	/**
	 * 根据借阅次数查询书的情况
	 * 
	 * @throws SQLException
	 */
	private void selectByCount() throws SQLException {
		// TODO Auto-generated method stub
		recordDao.selectByCount();

	}

	/**
	 * 根据借阅卡查询借阅记录
	 * 
	 * @throws SQLException
	 */
	private void selectByCardId() throws SQLException {
		// TODO Auto-generated method stub
		Scanner scanner6 = new Scanner(System.in);
		System.out.println("请输入要查询的卡的id：");
		String cardId = scanner6.nextLine();
		BorrowRecordModel borrowRecordModel = new BorrowRecordModel(cardId, null, 0);
		recordDao.selectList(borrowRecordModel);
//		scanner6.close();
	}

	/**
	 * 根据书查询借阅记录
	 * 
	 * @throws SQLException
	 */
	private void selectByBookId() throws SQLException {
		// TODO Auto-generated method stub
		Scanner scanner5 = new Scanner(System.in);
		System.out.println("请输入要查询的书的id：");
		String bookId = scanner5.nextLine();
		BorrowRecordModel borrowRecordModel = new BorrowRecordModel(null, bookId, 0);
		recordDao.selectList(borrowRecordModel);
//		scanner5.close();
	}

	/**
	 * 还书
	 * 
	 * @throws SQLException
	 */
	private void returnbook() throws SQLException {
		// TODO Auto-generated method stub
		Scanner scanner4 = new Scanner(System.in);
		System.out.println("还书请先输入借阅卡号：");
		String card = scanner4.nextLine();
		// 校验借阅卡
		CardModel cardModel = new CardModel(card);
		boolean flag = cardDao.select(cardModel);
		if (flag == false) {
			System.out.println("该卡尚未开通");
			return;
		}
		System.out.println("输入要还的书的编号：");
		String bookId = scanner4.nextLine();
		BorrowRecordModel borrowRecordModel = new BorrowRecordModel(card, bookId, 1);
		recordDao.insert(borrowRecordModel);
		System.out.println("还书完成");
//		scanner4.close();

	}

	/**
	 * 借书
	 * 
	 * @throws SQLException
	 */
	private void borrow() throws SQLException {
		// TODO Auto-generated method stub
		Scanner scanner3 = new Scanner(System.in);
		System.out.println("借书请先输入借阅卡号：");
		String card = scanner3.nextLine();
		// 校验借阅卡
		CardModel cardModel = new CardModel(card);
		boolean flag = cardDao.select(cardModel);
		if (flag == false) {
			System.out.println("该卡尚未开通");
			return;
		}
		System.out.println("输入要借的书的编号：");
		String bookId = scanner3.nextLine();
		BorrowRecordModel borrowRecordModel = new BorrowRecordModel(card, bookId, 0);
		BookModel bookModel = new BookModel(bookId, null);

		recordDao.insert(borrowRecordModel);
		System.out.println("借书完成");
//		scanner3.close();
	}

	/**
	 * 办理借阅卡
	 * 
	 * @throws SQLException
	 */
	private void insertCard() throws SQLException {
		// TODO Auto-generated method stub
		Scanner scanner2 = new Scanner(System.in);
		System.out.println("输入办理的卡号:");
		String cardString = scanner2.nextLine();
		CardModel cardModel = new CardModel(cardString);
		cardDao.insert(cardModel);
		System.out.println("办卡成功！");
//		scanner2.close();
	}

	/**
	 * 进书，并更新库存
	 * 
	 * @throws SQLException
	 */
	private void insertBook() throws SQLException {
		// TODO Auto-generated method stub
		List<BookModel> booklist = new ArrayList<BookModel>();
		List<BookStockModel> bookStocklist = new ArrayList<BookStockModel>();
		Scanner scanner1 = new Scanner(System.in);
		System.out.println("输入本次进书的数量：");
		String string = scanner1.nextLine();
		int n = Integer.valueOf(string);
		for (int i = 0; i < n; i++) {
			System.out.print("输入书号：");
			String bookid = scanner1.nextLine();
			System.out.print("输入书名：");
			String bookname = scanner1.nextLine();
			System.out.print("输入数量：");
			String string2 = scanner1.nextLine();
			int num = Integer.valueOf(string2);
			BookModel bookModel = new BookModel(bookid, bookname);
			BookStockModel bookStockModel = new BookStockModel(bookid, num);
			booklist.add(bookModel);
			bookStocklist.add(bookStockModel);
		}
		bookStockDao.update(bookStocklist);
		bookDao.insertList(booklist);
		System.out.println("进书完成");

	}

	public void print() {
		System.out.println("-----------欢迎来到本图书信息管理系统-----------");
		System.out.println("请输入按键进行相应的操作！");
		System.out.println("1:进书");
		System.out.println("2:办理借阅卡");
		System.out.println("3:借书");
		System.out.println("4:还书");
		System.out.println("5:查询某书的借阅记录");
		System.out.println("6:查询某卡的借阅记录");
		System.out.println("7:按借阅次数，显示图书信息");
		System.out.println("0:输入0,退出本系统！");
		System.out.println("---------------------------------------------");
	}

}
